//
//  QCNetworkTools.swift
//  SwiftDemoWithSina
//
//  Created by qu on 2017/2/4.
//  Copyright © 2017年 Apple. All rights reserved.
//

import UIKit
import AFNetworking

//请求枚举(可以支持字符串类型)
enum QCNetworkToolsMethod:String {
    case get = "get"
    case post = "post"
}

class QCNetworkTools: AFHTTPSessionManager {
    //创建一个全局访问点
    static let shared:QCNetworkTools = {
        
        let tools = QCNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
//        tools.requestSerializer.setValue("application/json", forHTTPHeaderField: "Content-Type")
        return tools
    }()
    //获取网络请求
    
    func request(method:QCNetworkToolsMethod,urlString:String,parameters:Any?,success:@escaping (Any?)->(),failure:@escaping (Error)->()){
        //get 请求
        if method == .get {
            self.get(urlString, parameters: parameters, progress: nil, success: { (_, res) in
                success(res)
            }, failure: { (_,err) in
                failure(err)
            })
        } else {
            //post 请求
            self.post(urlString, parameters: parameters, progress: nil, success: { (_, res) in
                success(res)
            }, failure: { (_, err) in
                failure(err)
            })
        }
        
        
    }
    
}

//发布微博(文字微博&图片微博)
extension QCNetworkTools {
    //文字微博
    func update(status: String,success:@escaping (Any?)->(),failure:@escaping (Error)->()){
        //请求的url
        let urlStr = "https://api.weibo.com/2/statuses/update.json"
        //请求参数
        let parmas:[String : Any] = [
            "access_token" : QCOAuthViewModel.shared.accessToken!,
            "status" : status
        ]
        //发送请求
        
        request(method: QCNetworkToolsMethod.post, urlString: urlStr, parameters: parmas, success: success, failure: failure)
    }
    //图片微博
    func upload(status:String,images:[UIImage],success:@escaping (Any?)->(),failure:@escaping (Error)->()){
        
        //请求的url
        let urlStr = "https://api.weibo.com/2/statuses/upload.json"
        //请求参数
        let parmas:[String : Any] = [
            "access_token" : QCOAuthViewModel.shared.accessToken!,
            "status" : status
        ]
        
        post(urlStr, parameters: parmas, constructingBodyWith: { (formdata) in
            /*
             - 参数01 上传的二进制文件
             - 参数02 服务器指定的名字 pic  mp4 mp3 区分客户端上传的是什么文件描述
             - 参数03 文件路径名字 一般可以随意些 及时你写了 服务器也不用
             - 参数04 告知服务器我们上传的文件的类型 一般可以传入application/octet-stream
             */
  
            for image in images {
        
               let data = UIImagePNGRepresentation(image)!
               formdata.appendPart(withFileData: data, name: "pic", fileName: "xxx", mimeType: "application/octet-stream")
                
            }
        }, progress: nil, success: { (_, res) in
            success(res)
        }) { (_, err) in
            failure(err)
        }
        
    }
}


//请求微博首页数据
extension QCNetworkTools {
    func loadHomeData(since_id:Int64, max_id:Int64,success:@escaping (Any?)->(),failure:@escaping (Error)->()){
        //请求地址
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        //请求参数
        let params = ["access_token":QCOAuthViewModel.shared.accessToken!,
                      "since_id":"\(since_id)",
                     "max_id":"\(max_id)"
        ]
        //发送请求
        request(method: QCNetworkToolsMethod.get, urlString: urlString, parameters: params, success: success, failure: failure)
    }
}

extension QCNetworkTools {
    func loadUserAccount(code:String,success:@escaping (Any?)->(),failure:@escaping (Error)->()){
    //请求获取accessToken
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id": APPKEY,
            "client_secret": APPSECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": REDIRECT_URI
        ]
        //发送请求
       request(method: QCNetworkToolsMethod.post, urlString: urlString, parameters: params, success: success, failure: failure)
        
    }
    func loadUserInfo(userAccountModel:QCUserAccountModel,success:@escaping (Any?)->(),failure:@escaping (Error)->()){
        // 请求的url
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 请求的参数
        let params = [
            "access_token":userAccountModel.access_token!,
            "uid": userAccountModel.uid!
        ]
        request(method: QCNetworkToolsMethod.get, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
}

